export const setSourceBtns = (editor, ResourceChooseRef) => {
  // 添加选择资源
  editor.ui.registry.addMenuItem('resourceselect', {
    icon: 'gallery',
    text: 'Select resource',
    onAction: () => {
      editor.dispatch('contexttoolbar-hide', { toolbarKey: 'editresource' })
      ResourceChooseRef.value.showDialog({
        type: '',
        multiple: true
      })
    }
  })
  // 添加菜单悬浮
  editor.ui.registry.addContextMenu('resourceselect', {
    update: element => {
      let list = ''
      if (element?.nodeName.toLowerCase() === 'img' || element?.previousSibling?.nodeName.toLowerCase() === 'video' || element?.previousSibling?.nodeName.toLowerCase() === 'audio') {
        list = 'resourceselect'
      }
      return list
    }
  })
  // 添加按钮
  editor.ui.registry.addButton('resourceselect', {
    icon: 'gallery',
    tooltip: 'Select resource',
    onAction: () => {
      editor.dispatch('contexttoolbar-hide', { toolbarKey: 'editresource' })
      ResourceChooseRef.value.showDialog({
        type: '',
        multiple: true
      })
    }
  })
  editor.ui.registry.addButton('removeresource', {
    icon: 'remove',
    tooltip: 'Delete resource',
    onAction: () => {
      editor.selection.setContent('')
      editor.dispatch('contexttoolbar-hide', { toolbarKey: 'editresource' })
    }
  })
  // 添加按钮悬浮
  editor.ui.registry.addContextToolbar('image', {
    predicate: function (node) {
      return node?.nodeName.toLowerCase() === 'img'
    },
    items: 'image',
    position: 'selection'
  })
  editor.ui.registry.addContextToolbar('media', {
    predicate: function (node) {
      return node?.childNodes.length > 0 && node?.childNodes[0]?.nodeName.toLowerCase() === 'video'
    },
    items: 'alignleft aligncenter alignright media',
    position: 'selection'
  })
  editor.ui.registry.addContextToolbar('editresource', {
    predicate: function (node) {
      return node?.nodeName.toLowerCase() === 'img' || (node?.childNodes.length > 0 && node?.childNodes[0].nodeName.toLowerCase() === 'video') || node?.nodeName.toLowerCase() === 'audio'
    },
    items: 'removeresource',
    position: 'selection'
  })
}
